$(function() {
	var uid = window.localStorage.getItem("user_id");
	// 订单渲染
	$.ajax({
		url: 'http://www.teaweilai.com/app/admin/Order/OrderShow',
		type: 'POST',
		dataType: 'json',
		data: {
			uid: uid
		},
		success: function(data) {
			console.log(data)
			if (data.sing.length == 0) {
				$(".allOrder").html("<h3 style='text-align: center;'>您还没有订单</h3>");
			} else {
				var strList = template("order", {
					order: data
				});
				$(".allOrder").html(strList);
				var sameGoods = $(".oneOrder goods");
				console.log(sameGoods)
					// for (var i = 0; i < sameGoods.length; i++) {
					// 	console.log(123)
					// 	if (sameGoods[i].attr('data-goods_id') == sameGoods[i + 1].attr('data-goods_id')) {
					// 		console.log(345)
					// 		sameGoods[i].remove();
					// 		sameGoods[i + 1].children('number').html() = Number(sameGoods[i + 1].children('number').html()) + 1;
					// 	}
					// }
			}
		}
	})

	// 订单删除
	$(document).on("click", ".shopTitle span", function() {
		$(".areYouSure").css({
			display: 'block'
		});
		var oid = $(this).eq(0).attr('data-order_id');
		var that = $(this);
		$(".sureS").click(function() {
			$.ajax({
				url: 'http://www.teaweilai.com/app/admin/Order/OrderDel',
				type: 'POST',
				dataType: 'json',
				data: {
					order_id: oid,
					uid: uid
				},
				success: function(data) {
					console.log(data);
					if (data.sing == 1) {
						console.log($(this));
						that.parents(".oneOrder").eq(0).remove();
					}
					$(".areYouSure").css({
						display: 'none'
					});
				}
			})
		})
	})
	$(".cancel").click(function() {
			$(".areYouSure").css({
				display: 'none'
			});
		})
		//确认收货
	$(document).on("click", ".sure", function() {
		var oid = $(this).parents(".oneOrder").eq(0).attr('data-order_id');
		var that = $(this);
		console.log(that);
		$.ajax({
			url: 'http://www.teaweilai.com/app/admin/Order/QueryOrder',
			type: 'POST',
			dataType: 'json',
			data: {
				uid: uid,
				order_id: oid
			},
			success: function(data) {
				console.log(data);
				if (data.code == 200) {
					that.eq(0).remove();
				}
			}
		})
	})

	// 取消订单
	$(document).on("click", ".cancelList", function() {
			var oid = $(this).parents(".oneOrder").eq(0).attr('data-order_id');
			var that = $(this);
			console.log(oid);
			$.ajax({
				url: 'http://www.teaweilai.com/app/admin/Order/UnOrder',
				type: 'POST',
				dataType: 'json',
				data: {
					uid: uid,
					order_id: oid
				},
				success: function(data) {
					console.log(data);
					if (data.sing == 1) {
						that.html("已取消");
					}
				}
			})
		})
		// 点击支付
	var order_id;
	/*微信支付的准备*/
	if (GetQueryString('openid') != null) {
		$(".payAgainDiv").css({
			display: "block"
		});
		$('#wx').attr('checked', 'checked');
	}
	$('#wx').click(function() {
		window.location.href = 'http://www.teaweilai.com/app/Admin/PaymentWx/getOpenid_list.html';
	})

	function GetQueryString(name) {
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
		var r = window.location.search.substr(1).match(reg);
		if (r != null) return unescape(r[2]);
		return null;
	}
	/* 微信支付准备结束*/

	$(document).on("click", ".payAgain", function() {
			// 弹出框，支付宝，微信
			$(".payAgainDiv").css({
				display: "block",
			});
			order_id = $(this).parents(".oneOrder").attr('data-order_id');
			window.sessionStorage.setItem("oid", order_id);
		})
		// 点击支付
	$(".paySure").click(function() {
			if ($(".payAgainDiv input:checked").next("span").eq(0).html() == "支付宝") {
				pay_id = 1;
				pay_name = "支付宝";
			} else {
				pay_id = 2;
				pay_name = "微信";
			}
			var openid = GetQueryString('openid');
			// alert(openid);
			if (pay_id == 2 && openid == '') {
				alert('no openid');
				return false
			} else {
				console.info(openid);
			}
			if (pay_id == 1) { // 支付宝支付
				$.ajax({
					url: 'http://www.teaweilai.com/app/admin/Payment/getCode',
					type: 'POST',
					dataType: 'json',
					data: {
						order_id: order_id
					},
					beforeSend: function() {
						// console.log(obj.out_trade_no);
					},
					success: function(data) {
						console.log(data);
						$("body").html(data);
					}
				})
			}
			if (pay_id == 2) { // 微信支付
				var order_id = window.sessionStorage.getItem("oid");
				$.ajax({
						url: 'http://www.teaweilai.com/app/Admin/PaymentWx/getCodeWx',
						type: 'POST',
						dataType: 'JSON',
						data: {
							order_id: order_id,
							openid: openid
						},
						beforeSend: function() {
							// alert(openid);
							console.log(order_id);
						}
					})
					.done(function(data) {
						WXobj = JSON.parse(data);

						function onBridgeReady() {
							// alert("微信支付")
							WeixinJSBridge.invoke(
								'getBrandWCPayRequest', WXobj,
								function(res) {
									//返回原因
									// alert(JSON.stringify(res));
									if (res.err_msg == "get_brand_wcpay_request:ok") {
										window.sessionStorage.removeItem("order_id");
										window.location.href = "http://www.teaweilai.com/app/App/html/paySuccess.html";
									} // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。 
									else {
										window.location.href = "http://www.teaweilai.com/app/App/html/payFail.html";
									}
								}
							);
						}
						if (typeof WeixinJSBridge == "undefined") {
							if (document.addEventListener) {

								document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
							} else if (document.attachEvent) {
								// alert('2');
								document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
								document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
							}
						} else {
							onBridgeReady();
						}
					})
					.fail(function() {
						console.log("error")
					})
			}
		})
		// 支付弹窗取消
	$(".payCancel").click(function() {
		$(".payAgainDiv").css({
			display: "none",
		});
	})

	// 订单详情
	$(document).on("click", ".seeDetail", function() {
		var poid = $(this).parents(".oneOrder").eq(0).attr('data-parent_order_id');
		window.sessionStorage.setItem("poid", poid);
		var shopName = $(this).parents(".oneOrder").find(".shops").eq(0).html();
		window.sessionStorage.setItem("shopN", shopName);
		window.location.href = "seeOrder.html";
	})

	// 点击店铺名，到店铺详情
	$(document).on("click", ".shops", function(e) {
			e.preventDefault();
			console.log($(this))
			var sid = $(this).eq(0).attr('data-seller_id');
			window.sessionStorage.setItem("sid", sid);
			window.location.href = "waterBusinessperson.html";
		})
		// 点击商品
	$(document).on("click", ".goods", function() {
		var gid = $(this).eq(0).attr('data-goods_id');
		window.sessionStorage.setItem("gid", gid);
		window.location.href = "tea_detail.html";
	})

})